<?php
namespace admin\Model;
require_once 'databases.php';
use controller\databases\database;

/**
* 模型处理
*/
class model{

	public function login($a,$b)
	{
		session_start();
		if(!isset($a) || !isset($b) || trim($a)=='' || trim($b)==''){
			exit(json_encode(array('status'=>-1,'msg'=>'用户名或密码错误！')));
		}else{
			$database = new database();
			$sql = "select * from admin where name='".$a."'";
			$test = $database->select($sql);
			if($test[0]['password'] != $b){
				exit(json_encode(array('status'=>-1,'msg' => '用户名或密码错误！')));
			}
			$_SESSION['admin']=$a;
			exit(json_encode(array('status'=>1,'msg' => '登录成功！','url' => '/mian.html')));
		}
	}
	//查询数据集
	public function json_table($r,$p,$sidx,$sord)
	{
		$databases = new database();
		$sql = "select * from user order by `".$sidx."` ".$sord." limit ".($p-1)*$r."," .($p*$r);
		$sqls = "select * from user";
		return $databases->limit($sql,$sqls);	
	}

	//查询数据集
	public function json_table_user($r,$p,$sidx,$sord)
	{
		$databases = new database();
		$sql = "select a.id,a.name,a.sfz,a.addr,b.d_name,c.basic from user_info as a left join depart as b on a.depart_id=b.id left join user as c on a.id=c.user_id order by `".$sidx."` ".$sord." limit ".($p-1)*$r."," .($p*$r);
		//$sql = "select * from user order by `".$sidx."` ".$sord." limit ".($p-1)*$r."," .($p*$r);
		// $sql = 'select a.id,a.name,a.sfz,a.addr,b.name,c.basic from user_info as a left join depart as b on a.depart_id=b.id left join user as c on a.id=c.user_id';
		$sqls = "select * from user_info";
		return $databases->limit_user($sql,$sqls);	
	}
	//搜索
	public function search($table,$str,$search)
	{
		$sqls = "select * from ".$table."";
		$sql = "select * from ".$table." where ".$search."="."'".$str."'";
		$databases = new database();
		return $databases->limit($sql,$sqls);
	}

	// 用户查询
	public function user_seach($str,$search)
	{

		$sql = "select a.user_id,a.name,a.on_day,a.add,a.fk,a.month,a.pay,c.d_name from user as a left join user_info as b on a.user_id = b.id left join depart as c on b.depart_id=c.id"." where ".$str."="."'".$search."'";
		$databases = new database();
		$sqls = "select * from user";
		return $databases->limit($sql,$sqls);
	}

	//搜索用户
	public function search_user ($str,$search)
	{
		$sqls = "select * from user_info";
		$sql = "select * from user_info where ".$search."="."'".$str."'";
		$databases = new database();
		return $databases->limit($sql,$sqls);
	}
	//删除数据
	public function del($table,$id)
	{
		$databases = new database();
		$sql="delete from ".$table." where id=".$id;
		$databases->sql($sql);
	}

	//修改数据
	public function update($table,$id,$name,$m_day,$on_day,$on_abs,$is_tr,$is_le,$add,$fk,$basic,$pay,$user_id)
	{
		$databases = new database();
		$sql = "update ".$table." set "."`name`="."'".$name."',`m_day`="."'".$m_day."',`on_day`='".$on_day."',`on_abs`='".$on_abs."',`is_tr`='".$is_tr."',`is_le`='".$is_le."',`add`='".$add."',`fk`='".$fk."',`basic`='".$basic."',`pay`='".$pay."',`user_id`='".$user_id."' where id=".$id;
		$databases->sql($sql);
	}
	
	public function user_update($table,$id,$name,$d_name,$sfz,$addr)
	{
		$databases = new database();
		$sql = "update ".$table." set "."`name`="."'".$name."',`depart_id`="."'".$d_name."',`sfz`='".$sfz."',`addr`='".$addr."' where id=".$id;
		$databases->sql($sql);
	}
	//添加数据
	public function add($table,$name,$m_day,$on_day,$on_abs,$is_tr,$is_le,$add,$fk,$basic,$pay,$m,$user_id)
	{
		$databases = new database();
		$sql = 'insert `'.$table.'`(`id`,`name`,`m_day`,`on_day`,`on_abs`,`is_tr`,`is_le`,`add`,`fk`,`basic`,`pay`,`month`,`user_id`) values ('.'null' .",'".$name."','".$m_day."','".$on_day."','".$on_abs."','".$is_tr."','".$is_le."','".$add."','".$fk."','".$basic."','".$pay."','".$m."','".$user_id."'".')';
		$databases->sql($sql);
	}

	public function user_add($table,$id,$d_name,$name,$sfz,$addr)
	{
		$databases = new database();
		$sql = 'insert `'.$table.'`(`id`,`depart_id`,`name`,`sfz`,`addr`) values ('.'null' .",'".$d_name."','".$name."','".$sfz."','".$addr."'".')';
		$databases->sql($sql);
	}
	// 退出登录
	public function logout($a)
	{
		session_start();
		if(isset($_SESSION['admin']) && $_SESSION['admin']==$a){
			session_unset();//free all session variable
            session_destroy();//销毁一个会话中的全部数据
            setcookie(session_name(),'',time()-3600);//销毁与客户端的卡号
            return $url = "login.html";
         	
		}
	}
	public function user_info($user_id)
	{
		$database = new database();
		$sql = 'select * from user_info where `id`='.$user_id;
		return $database->select($sql);
	}

	public function depart($r,$p,$sidx,$sord)
	{
		$databases = new database();
		$sql = "select * from depart order by `".$sidx."` ".$sord." limit ".($p-1)*$r."," .($p*$r);
		$sqls = "select * from depart";
		return $databases->limit_user($sql,$sqls);	
	}

	public function update_depart($table,$id,$name)
	{
		$databases = new database();
		$sql = "update ".$table." set "."`d_name`="."'".$name."'"." where id=".$id;
		$databases->sql($sql);
	}

	public function depart_add($table,$id,$name)
	{
		$databases = new database();
		$sql = 'insert `'.$table.'`(`id`,`d_name`) values ('.$id .",'".$name."'".')';
		var_dump($sql);
		$databases->sql($sql);
	}

	public function depart_seach($str,$search)
	{
		$sqls = "select * from depart";
		$sql = "select * from depart where ".$search."="."'".$str."'";
		$databases = new database();
		return $databases->limit($sql,$sqls);
	}

	public function d_search()
	{
		$sql = "select * from depart";
		$databases = new database();
		return $databases->select($sql);
	}
}